package Service;

import facecat.topin.service.*;
import facecat.topin.core.*;
import java.util.*;

public class LatestServiceClient extends FCClientService{
    public LatestServiceClient() {
        setServiceID(SERVICEID);
    }

    public int SERVICEID = 10003;

    //开始推送
    public int FUNCTION_SUBCODES = 0;

    //取消推送
    public int FUNCTION_UNSUBCODES = 1;

    // 最新数据
    public static int FUNCTION_NEWDATA = 2;

    public boolean m_connected = false;

    private int m_socketID;

    /*
     * 获取套接字
     */
    public int getSocketID() {
        return m_socketID;
    }

    /*
     * 设置套接字
     */
    public void setSocketID(int value) {
        m_socketID = value;
    }

    /*
     * 注册代码
     *@param requestID 请求ID
     *@param codes 代码
     */
    public void subCodes(int requestID, String codes) {
        FCBinary bw = new FCBinary();
        try{
        bw.writeString(codes);
        byte[] bytes = bw.getBytes();
        int ret = send(new FCMessage(getServiceID(), FUNCTION_SUBCODES, requestID, m_socketID, 0, getCompressType(), bytes.length, bytes));
        bw.close();
        }catch(Exception ex){
            
        }
    }

    /*
     * 反注册代码
     *param requestID 请求ID
     */
    public void unSubCodes(int requestID) {
        FCBinary bw = new FCBinary();
        try{
            bw.writeString("1");
            byte[] bytes = bw.getBytes();
            int ret = send(new FCMessage(getServiceID(), FUNCTION_UNSUBCODES,  requestID, m_socketID, 0, getCompressType(), bytes.length, bytes));
            bw.close();}
        catch(Exception ex){
            
        }
    }

    /*
     * 获取最新数据
     *@param body 包体
     *@param bidyLength 包体长度
     */
    public static SecurityLatestData getLatestData(byte[] body, int bodyLength) {
        FCBinary br = new FCBinary();
        try{
        br.write(body, bodyLength);
        SecurityLatestData latestData = new SecurityLatestData();
        latestData.m_code = br.readString();
        latestData.m_open = br.readFloat();
        latestData.m_lastClose = br.readFloat();
        latestData.m_close = br.readFloat();
        latestData.m_high = br.readFloat();
        latestData.m_low = br.readFloat();
        latestData.m_volume = br.readDouble();
        latestData.m_amount = br.readDouble();
        latestData.m_buyPrice1 = br.readFloat();
        latestData.m_buyVolume1 = br.readInt();
        latestData.m_buyPrice2 = br.readFloat();
        latestData.m_buyVolume2 = br.readInt();
        latestData.m_buyPrice3 = br.readFloat();
        latestData.m_buyVolume3 = br.readInt();
        latestData.m_buyPrice4 = br.readFloat();
        latestData.m_buyVolume4 = br.readInt();
        latestData.m_buyPrice5 = br.readFloat();
        latestData.m_buyVolume5 = br.readInt();
        latestData.m_sellPrice1 = br.readFloat();
        latestData.m_sellVolume1 = br.readInt();
        latestData.m_sellPrice2 = br.readFloat();
        latestData.m_sellVolume2 = br.readInt();
        latestData.m_sellPrice3 = br.readFloat();
        latestData.m_sellVolume3 = br.readInt();
        latestData.m_sellPrice4 = br.readFloat();
        latestData.m_sellVolume4 = br.readInt();
        latestData.m_sellPrice5 = br.readFloat();
        latestData.m_sellVolume5 = br.readInt();
        latestData.m_turnoverRate = br.readFloat();
        latestData.m_pe = br.readFloat();
        latestData.m_marketValue = br.readFloat();
        latestData.m_flow = br.readFloat();
        latestData.m_ratio = br.readFloat();
        latestData.m_equity = br.readDouble();
        latestData.m_flowEquity = br.readDouble();
        latestData.m_upperLimit = br.readFloat();
        latestData.m_lowerLimit = br.readFloat();
        latestData.m_date = br.readDouble();
        br.close();
        return latestData;
        }catch(Exception ex){
            return null;
        }
    }

    /*
     * 接收消息方法
     */
    public void onReceive(FCMessage message) {
        super.onReceive(message);
        sendToListener(message);
    }

    /*
     * 客户端关闭
     */
    public void onClientClose(int socketID, int localSID)
    {
        super.onClientClose(socketID, localSID);
        m_connected = false;
    }
}
